package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hospitalManage.entity.Dict;
import com.example.hospitalManage.service.DictService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ProjectName: yygh
 * @Author: gxy
 * @Time: 2023/1/2 11:57
 * @Description: 描述
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
@Api(value = "v1",tags = "数据字典查询相关接口")
public class DictQueryController {

    @Autowired
    private DictService dictService;

    @GetMapping(value = "/dict/{code}")
    @ApiOperation("根据id查询字典值")
    public ResponseResult getValueByCode(@PathVariable Long code) {
        Dict dict = dictService.getById(code);
        return Response.createOkResp(dict);
    }

    @GetMapping(value = "/dict/getCode/{name}")
    @ApiOperation("根据值查询对应字典编码")
    public ResponseResult getCodeByValue(@PathVariable String name){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Dict dictQuery = dictService.getOne(wrapper);
        return Response.createOkResp(dictQuery);
    }

    @GetMapping(value = "/dict/getChildren/{parentId}")
    @ApiOperation("根据父id查询对应值列表")
    public ResponseResult<List<Dict>> getDictByParentId(@PathVariable String parentId){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<Dict> dictQuery = dictService.list(wrapper);
        return Response.createOkResp(dictQuery);
    }

    @GetMapping(value = "/dict/getDict/{dictCode}")
    @ApiOperation("根据字典代码查询对应字典")
    public ResponseResult getByDictCode(@PathVariable String dictCode){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",dictCode);
        Dict dictQuery = dictService.getOne(wrapper);
        return Response.createOkResp(dictQuery);
    }





}
